package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
* 执行DQL语句
* */
public class JDBCDemo5 {
    public static void main(String[] args) {
        try (
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql ="SELECT id,name,salary,title "+
                        "FROM teacher";
             /*
                Statement提供的用于执行DQL语句的方法:
                ResultSet executeQuery(String sql)
             */
            ResultSet rs =statement.executeQuery(sql);
            /*
                ResultSet提供的遍历结果集相关方法:
                boolean next()
                让结果集向下移动一条记录，如果该记录存在则返回true
                默认结果集在第一条记录之上

                int getInt(String cn)
                String getString(String cn)
                long getLong(String cn)
                double getDouble(String cn)
                ...
                上述方法用于获取指定字段的值，具体调用那个方法却决于字段的数据类型
             */
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int salary = rs.getInt("salary");
                String title = rs.getString("title");
                System.out.println(id+",\t"+name+",\t "+salary+",\t "+title+"\t");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
